<template>

  <!--头部公用组件-->
  <!--编辑、发表 都会一直显示  可以尝试自己的想法  引入那个哈 -->
  <!--还可以来个后台管理哦哦哦-->
  <!--  ！！！！其他页面要导入这个Header的方法：
        例如blogs页面  在script中导入
    -->
  <div class="m-content">

    <h3>欢迎来到CQUPT博客</h3>
    <div class="block">
      <el-avatar :size="50" :src="user.avatar"></el-avatar>
      <div>{{ user.username }}</div>
    </div>

    <div class="maction">
      <span><el-link href="/blogs">主页</el-link></span>


      <el-divider direction="vertical"></el-divider>
      <span v-show="!hasLogin"><el-link type="primary" href="/#/login">登录</el-link></span>
      <span v-show="!hasLogin"><el-divider direction="vertical"></el-divider> </span>
      <span v-show="!hasLogin"><el-link type="primary" href="/#/register">注册</el-link></span>

      <span v-show="hasLogin"><el-link type="danger" @click="logout">退出</el-link></span>
    </div>

  </div>
</template>

<script>
export default {
  name: "HeaderView",
  data() {
    return {
      user: {
        username: '请先登录',
        avatar: ''
      },
      hasLogin: false,

    }
  },
  methods: {
    logout() {
      const _this = this
      _this.$axios.get("/logout", {
        headers: {"Authorization": localStorage.getItem("token")}
      }).then(res => {
        _this.$store.commit("REMOVE_INFO")
        // this.hasLogin = false
        // this.user.username = '请先登录'
        // this.avatar = ''
        _this.$router.push("/")
        // 这里有bug  我在blogs页面 logout 又push到/blog   Navigating to current location ("/blogs") is not allowed
        //解决办法： https://blog.csdn.net/weixin_43606809/article/details/103040805

        //发现图片不能刷新  手动
        location.reload();
      })
    },


  },
  created() {
    if (this.$store.getters.getUser.username != null) {
      console.log(this.$store.getters.getUser.username)
      this.user.username = this.$store.getters.getUser.username
      this.user.avatar = this.$store.getters.getUser.avatar

      this.hasLogin = true
    }

  }
}
</script>

<style scoped>

.m-content {
  max-width: 960px;
  margin: 0 auto;
  text-align: center;

}

.maction {
  margin: 10px 0;
}


</style>